﻿using System;
using DataAccess.Repository;
using DataAccess.Models;

namespace KPI
{
	public partial class ProgressEdit : BasePage
	{
		private IKPIProgressRepository _KPIProgressRepository;
		private IKPIRepository _KPIRepository;
		private DataAccess.Models.KPI _TheKPI;
		private int _TheKPIID;

		private void BindData()
		{
			if(this.TheKPI != null)
			{
				this.lKPIContent.Text = this.TheKPI.KPIContent;
				string season = CommonHelper.GetSeason();
				if(this.TheKPI.TheKPIProgress != null)
				{
					switch(season)
					{
						case "一季度":
							this.CKEditorProgressContent.Text = this.TheKPI.TheKPIProgress.Quarter1Progress ?? string.Empty;
							break;

						case "二季度":
							this.CKEditorProgressContent.Text = this.TheKPI.TheKPIProgress.Quarter2Progress ?? string.Empty;
							break;

						case "三季度":
							this.CKEditorProgressContent.Text = this.TheKPI.TheKPIProgress.Quarter3Progress ?? string.Empty;
							break;

						case "四季度":
							this.CKEditorProgressContent.Text = this.TheKPI.TheKPIProgress.Quarter4Progress ?? string.Empty;
							break;
					}
				}
			}
		}

		protected void cusvProgressContent_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			try
			{
				args.IsValid = (args.Value.Length <= 300);
			}
			catch
			{
				args.IsValid = false;
			}
		}

		protected void btnCancel_Click(object sender, EventArgs e)
		{
			base.Response.Redirect("~/KPIList.aspx?id=" + this.TheKPI.GroupID.ToString());
		}

		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			if(this.Page.IsValid && (this.TheKPI != null))
			{
				KPIProgressForUpdate theKPIProgressForUpdate = new KPIProgressForUpdate
				{
					KPIID = this.TheKPIID,
					UpdateKPIProgressTime = DateTime.Now
				};
				switch(CommonHelper.GetSeason())
				{
					case "一季度":
						theKPIProgressForUpdate.UpdatedQuarter1Progress = this.CKEditorProgressContent.Text;
						break;

					case "二季度":
						theKPIProgressForUpdate.UpdatedQuarter2Progress = this.CKEditorProgressContent.Text;
						break;

					case "三季度":
						theKPIProgressForUpdate.UpdatedQuarter3Progress = this.CKEditorProgressContent.Text;
						break;

					case "四季度":
						theKPIProgressForUpdate.UpdatedQuarter4Progress = this.CKEditorProgressContent.Text;
						break;
				}
				if(this._KPIProgressRepository == null)
				{
					this._KPIProgressRepository = new KPIProgressRepository();
				}
				if(this._KPIProgressRepository.SaveKPIProgressForUpdate(theKPIProgressForUpdate))
				{
					this.TheKPI.PreState = this.TheKPI.CurrentState;
					this.TheKPI.CurrentState = KPIStates.Progress_Of_Modification_Modified_Review_Pending; //"progress_of_modification_modified_review_pending";
					if(this._KPIRepository.UpdateKPI(this.TheKPI, false))
					{
						CommonHelper.MessegeToAdmin(TheCurrentLoginUser.DeptName + " 进度已修改, 请您审核；您可以点击下面链接进行审核:", KPISiteContext.Current.RequestURL + "ProgressDetail.aspx?kpiid=" + this.TheKPI.KPIID.ToString());

						Response.Redirect("~/KPIList.aspx?id=" + this.TheKPI.GroupID.ToString());
					}
					else
					{
						ShowMessage("出错了,请稍后再试或联系管理员或技术人员.", true);
					}
				}
				else
				{
					ShowMessage("出错了,请稍后再试或联系管理员或技术人员.", true);
				}
			}
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			if(!base.IsPostBack)
			{
				bool flag = false;
				//LoginUserInfo currentUser = KPISiteContext.Current.CurrentUser;
				if(((TheCurrentLoginUser != null) && TheCurrentLoginUser.RoleName.Equals("unitadministrators")) && (this.TheKPI != null))
				{
					flag = ((this.TheKPI.CurrentState == KPIStates.Progress_Of_Modification_Request_Review_Approved) || (this.TheKPI.CurrentState == KPIStates.Progress_Of_Modification_Modified_Review_Pending)) || (this.TheKPI.CurrentState == KPIStates.Progress_Of_Modification_Modified_Review_Not_Approved);
				}
				if(!flag)
				{
					base.Response.Redirect("~/KPIList.aspx?groupid=" + this.TheKPI.GroupID.ToString());
				}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!base.IsPostBack)
			{
				this.lSeason.Text = CommonHelper.GetSeason();
				new CKFinder.FileBrowser
				{
					BasePath = "/ckfinder/",
					DisableThumbnailSelection = true
				}.SetupCKEditor(this.CKEditorProgressContent);
				this.BindData();
			}
		}

		protected void Page_UnLoad(object sender, EventArgs e)
		{
			if(this._KPIRepository != null)
			{
				((IDisposable)this._KPIRepository).Dispose();
			}
			if(this._KPIProgressRepository != null)
			{
				((IDisposable)this._KPIProgressRepository).Dispose();
			}
			if(CKEditorProgressContent != null)
			{
				CKEditorProgressContent.Dispose();
			}
		}

		public DataAccess.Models.KPI TheKPI
		{
			get
			{
				if((this._TheKPI == null) && (this.TheKPIID > 0))
				{
					if(this._KPIRepository == null)
					{
						this._KPIRepository = new KPIRepository();
					}
					this._TheKPI = this._KPIRepository.GetTheKPI(this.TheKPIID);
				}
				return this._TheKPI;
			}
		}

		public int TheKPIID
		{
			get
			{
				if(this._TheKPIID <= 0)
				{
					this._TheKPIID = CommonHelper.QueryStringInt("kpiid");
				}
				return this._TheKPIID;
			}
		}

	}
}